package com.example.coinhelper.db;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.coinhelper.Bean.Type;
import com.example.coinhelper.R;

import java.util.ArrayList;
import java.util.List;

public class TypeDBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "type.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "types_table";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_TYPE_NAME = "typeName";
    private static final String COLUMN_IMAGE_ID = "imageId";
    private static final String COLUMN_SELECTED_IMAGE_ID = "sImageId";
    private static final String COLUMN_KIND = "kind";

    public TypeDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableQuery = "CREATE TABLE " + TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_TYPE_NAME + " TEXT, " +
                COLUMN_IMAGE_ID + " INTEGER, " +
                COLUMN_SELECTED_IMAGE_ID + " INTEGER, " +
                COLUMN_KIND + " INTEGER)";

        db.execSQL(createTableQuery); // 执行表的创建

        // 这里仅在数据库首次创建时插入数据
        if (isFirstTimeCreation(db)) {
            insertInitialData(db);
        }
    }

    // 检查是否为数据库的首次创建
    private boolean isFirstTimeCreation(SQLiteDatabase db) {
        // 在这里添加逻辑，检查表中是否已经存在数据，用来确定是否为数据库的首次创建
        // 如果是首次创建，返回 true；否则返回 false
        // 例如，可以检查表中是否有任何数据来判断
        String countQuery = "SELECT COUNT(*) FROM " + TABLE_NAME;
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.moveToFirst();
        int count = cursor.getInt(0);
        cursor.close();
        return count == 0;
    }
    private void insertInitialData(SQLiteDatabase db) {
        String sql = "INSERT INTO " + TABLE_NAME + "(typeName, imageId, sImageId, kind) VALUES (?, ?, ?, ?)";
        // 执行数据插入操作
        // ...
        db.execSQL(sql , new Object[]{"餐饮" , R.mipmap.ic_canyin , R.mipmap.ic_canyin_fs , 0});
        db.execSQL(sql , new Object[]{"交通" , R.mipmap.ic_jiaotong , R.mipmap.ic_jiaotong_fs , 0});
        db.execSQL(sql , new Object[]{"购物" , R.mipmap.ic_gouwu , R.mipmap.ic_gouwu_fs , 0});
        db.execSQL(sql , new Object[]{"服饰" , R.mipmap.ic_fushi , R.mipmap.ic_fushi_fs , 0});
        db.execSQL(sql , new Object[]{"日用品" , R.mipmap.ic_riyongpin , R.mipmap.ic_riyongpin_fs , 0});
        db.execSQL(sql , new Object[]{"娱乐" , R.mipmap.ic_yule , R.mipmap.ic_yule_fs , 0});
        db.execSQL(sql , new Object[]{"零食" , R.mipmap.ic_lingshi , R.mipmap.ic_lingshi_fs , 0});
        db.execSQL(sql , new Object[]{"烟酒茶" , R.mipmap.ic_yanjiu , R.mipmap.ic_yanjiu_fs, 0});
        db.execSQL(sql , new Object[]{"学习" , R.mipmap.ic_xuexi , R.mipmap.ic_xuexi_fs , 0});
        db.execSQL(sql , new Object[]{"医疗" , R.mipmap.ic_yiliao , R.mipmap.ic_yiliao_fs , 0});
        db.execSQL(sql , new Object[]{"住宅" , R.mipmap.ic_zhufang , R.mipmap.ic_zhufang_fs , 0});
        db.execSQL(sql , new Object[]{"水电煤" , R.mipmap.ic_shuidianfei , R.mipmap.ic_shuidianfei_fs , 0});
        db.execSQL(sql , new Object[]{"通讯" , R.mipmap.ic_tongxun , R.mipmap.ic_tongxun_fs , 0});
        db.execSQL(sql , new Object[]{"人情来往" , R.mipmap.ic_renqingwanglai , R.mipmap.ic_renqingwanglai_fs , 0});
        db.execSQL(sql , new Object[]{"其他" , R.mipmap.ic_qita , R.mipmap.ic_qita_fs , 0});

        db.execSQL(sql , new Object[]{"薪资" , R.mipmap.in_xinzi , R.mipmap.in_xinzi_fs , 1});
        db.execSQL(sql , new Object[]{"奖金" , R.mipmap.in_jiangjin , R.mipmap.in_jiangjin_fs , 1});
        db.execSQL(sql , new Object[]{"借入" , R.mipmap.in_jieru , R.mipmap.in_jieru_fs , 1});
        db.execSQL(sql , new Object[]{"收债" , R.mipmap.in_shouzhai , R.mipmap.in_shouzhai_fs , 1});
        db.execSQL(sql , new Object[]{"利息收入" , R.mipmap.in_lixifuji , R.mipmap.in_lixifuji_fs , 1});
        db.execSQL(sql , new Object[]{"投资回报" , R.mipmap.in_touzi , R.mipmap.in_touzi_fs , 1});
        db.execSQL(sql , new Object[]{"二手交易" , R.mipmap.in_ershoushebei , R.mipmap.in_ershoushebei_fs , 1});
        db.execSQL(sql , new Object[]{"意外所得" , R.mipmap.in_yiwai , R.mipmap.in_yiwai_fs , 1});
        db.execSQL(sql , new Object[]{"其他" , R.mipmap.in_qt , R.mipmap.in_qt_fs , 1});
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 如果需要更新表结构，在此处执行相应的操作
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    // 插入类型数据
//
    @SuppressLint("Range")
    public List<Type> getTypesByKind(int kind) {
        List<Type> typesList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();

        // 查询数据库以获取特定类型的记录
        Cursor cursor = db.query(
                TABLE_NAME,
                new String[]{COLUMN_ID, COLUMN_TYPE_NAME, COLUMN_IMAGE_ID, COLUMN_SELECTED_IMAGE_ID, COLUMN_KIND},
                COLUMN_KIND + " = ?",
                new String[]{String.valueOf(kind)},
                null,
                null,
                null
        );

        // 如果存在符合条件的记录，则将其添加到类型列表中
        if (cursor != null && cursor.moveToFirst()) {
            do {
                int typeId = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
                String typeName = cursor.getString(cursor.getColumnIndex(COLUMN_TYPE_NAME));
                int imageId = cursor.getInt(cursor.getColumnIndex(COLUMN_IMAGE_ID));
                int selectedImageId = cursor.getInt(cursor.getColumnIndex(COLUMN_SELECTED_IMAGE_ID));
                int typeKind = cursor.getInt(cursor.getColumnIndex(COLUMN_KIND));

                // 创建类型对象并将其添加到列表中
                Type type = new Type(typeId, typeName, imageId, selectedImageId, typeKind);
                typesList.add(type);
            } while (cursor.moveToNext());

            // 关闭游标
            cursor.close();
        }

        // 关闭数据库连接
        db.close();

        // 返回具有特定类型的类型列表
        return typesList;
    }

//    @SuppressLint("Range")
//    // 查询所有类型数据
//    public List<Type> getAllTypes() {
//        List<Type> typeList = new ArrayList<>();
//        SQLiteDatabase db = this.getReadableDatabase();
//        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
//        if (cursor.moveToFirst()) {
//            do {
//                Type type = new Type();
//                type.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
//                type.setTypeName(cursor.getString(cursor.getColumnIndex(COLUMN_TYPE_NAME)));
//                type.setImageId(cursor.getInt(cursor.getColumnIndex(COLUMN_IMAGE_ID)));
//                type.setsImageId(cursor.getInt(cursor.getColumnIndex(COLUMN_SELECTED_IMAGE_ID)));
//                type.setKind(cursor.getInt(cursor.getColumnIndex(COLUMN_KIND)));
//                typeList.add(type);
//            } while (cursor.moveToNext());
//        }
//
//        cursor.close();
//        db.close();
//        return typeList;
//    }
}

